﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BamM.WServices.QS;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;
using System.Web.Helpers;
using System.Dynamic;

namespace bamM.Controllers
{
    public class HomeController : BaseController
    {

        public ActionResult DisplayInstanceData(string viewName, string activityName, string queryName)
        {
            InstanceQuery query = new InstanceQuery();
            
            string queryStr = System.IO.File.ReadAllText(@"C:\bamM\" + viewName + "\\" + activityName + "\\" + queryName);
            var xmlDoc = new XmlDocument();
            using (StringReader s = new StringReader(queryStr))
            {
                xmlDoc.Load(s);
            }

            var ns = new XmlNamespaceManager(xmlDoc.NameTable);
            ns.AddNamespace("bam", "http://schemas.microsoft.com/BizTalkServer/2004/10/BAM/SavedQuery");

            XmlNodeList nodes = xmlDoc.SelectNodes(string.Format("//bam:SelectClauses/bam:Column"), ns);

            List<string> columns = new List<string>();
            string result = string.Empty;                        
            List<WebGridColumn> columns1 = new List<WebGridColumn>();                                                
            
            
            query.SelectClauses = columns.Cast<string>().ToArray();
            BamM.WServices.QS.BamQueryServiceSoapClient client = new BamM.WServices.QS.BamQueryServiceSoapClient();
            
            Column[][] resultRows = client.GetInstanceData(viewName, activityName, query, 80);
                  
            foreach(Column[] row in resultRows)
            {
                result = result + "<tr>";
                dynamic expandoObject = new ExpandoObject();
                var dictionaryExpandoObject = (IDictionary<string, object>)expandoObject;

                foreach (Column col in row)
                {                    
                    dictionaryExpandoObject.Add(col.Name, col.Value);
                    
                }
                //finally add each ExpandoObject to list
                dataList.Add(dictionaryExpandoObject);
            }
            
            return View(dataList);
        }

        List<dynamic> dataList = new List<dynamic>();
        public ActionResult Index()
        {

            string viewName = "orders"; string activityName = "orderDetails";
            string queryName = "Default.xml";
            InstanceQuery query = new InstanceQuery();

            string queryStr = System.IO.File.ReadAllText(@"C:\bamM\" + viewName + "\\" + activityName + "\\" + queryName);
            var xmlDoc = new XmlDocument();
            using (StringReader s = new StringReader(queryStr))
            {
                xmlDoc.Load(s);
            }

            var ns = new XmlNamespaceManager(xmlDoc.NameTable);
            ns.AddNamespace("bam", "http://schemas.microsoft.com/BizTalkServer/2004/10/BAM/SavedQuery");

            XmlNodeList nodes = xmlDoc.SelectNodes(string.Format("//bam:SelectClauses/bam:Column"), ns);
            List<string> columns = new List<string>();
            foreach (XmlNode node in nodes)
            {
                columns.Add(node.InnerText);
            }
            
            string result = string.Empty;
            List<WebGridColumn> columns1 = new List<WebGridColumn>();


            query.SelectClauses = columns.Cast<string>().ToArray();
            BamM.WServices.QS.BamQueryServiceSoapClient client = new BamM.WServices.QS.BamQueryServiceSoapClient();

            //Column[][] resultRows = client.GetInstanceData(viewName, activityName, query, 80);

            //foreach (Column[] row in resultRows)
            //{
            //    result = result + "<tr>";
            //    dynamic expandoObject = new ExpandoObject();
            //    var dictionaryExpandoObject = (IDictionary<string, object>)expandoObject;

            //    foreach (Column col in row)
            //    {
            //        dictionaryExpandoObject.Add(col.Name, col.Value);

            //    }
            //    //finally add each ExpandoObject to list
            //    dataList.Add(dictionaryExpandoObject);
            //}

            return View(dataList);
        }


        
    }

}